package simulate;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: tao
 * Date: 2023-12-17
 * Time: 10:31
 */
public class MyArrayList implements IList{
    public int[] elem;
    private int usedSize = 0;
    private int DEFAULT_SIZE = 10;

    public MyArrayList() {
        elem = new int[DEFAULT_SIZE];
    }
    public MyArrayList(int size) {
        elem = new int[size];
    }
    @Override
    public void add(int data) {
        //检查容量
        checkCapacity();
        //添加元素
        elem[usedSize] = data;
        usedSize++;
    }
    private void checkCapacity() {//检查容量
        if(ifFull()) {
            //扩容
            elem = Arrays.copyOf(elem, elem.length * 2);
        }
    }
    private boolean ifFull() {//判断数组满了没
        return usedSize == elem.length;
    }

    @Override
    public void add(int pos, int data) throws PosIllegality{
        //判断pos下标是否合法
        try {
            checkPosOnAdd(pos);
        } catch (PosIllegality e) {
            e.printStackTrace();
        }
        //数组满了就要扩容
        checkCapacity();
        for (int i = usedSize - 1; i >= pos; i--) {
            elem[i + 1] = elem[i];
        }
        elem[pos] = data;
        usedSize++;
    }
    private void checkPosOnAdd(int pos) throws PosIllegality{
        if(pos < 0 || pos > usedSize) {
            //不合法，抛异常
            System.out.println("不合法");
            throw new PosIllegality("数组下标不合法" + pos);
        }
    }
    @Override
    public boolean contains(int toFind) {
        if(usedSize == 0) return false;
        for (int i = 0; i < usedSize; i++) {
            if(elem[i] == toFind) {
                return true;
            }
        }
        return false;
    }

    @Override
    public int indexOf(int toFind) {
        if(usedSize == 0) return -1;
        for (int i = 0; i < usedSize; i++) {
            if(elem[i] == toFind) {
                return i;
            }
        }
        return -1;
    }

    @Override
    public int get(int pos) {
        try {
            checkPosOnGetAndSet(pos);
        } catch (PosIllegality e) {
            e.printStackTrace();
        }
        if(usedSize == 0) {
            //数组为空，拿不了，抛异常
            throw new MyArrayListEmpty("顺序表为空");
        }
        return elem[pos];
    }
    private void checkPosOnGetAndSet(int pos) throws PosIllegality{
        if(pos < 0 || pos >= usedSize) {
            //下标不合法
            System.out.println("下标不合法");
            throw new PosIllegality("查找的下标不合法" + pos);
        }
    }

    @Override
    public void set(int pos, int value) throws PosIllegality{
        try {
            //检查下标合法性
            checkPosOnGetAndSet(pos);
        } catch (PosIllegality e) {
            e.printStackTrace();
        }
        elem[pos] = value;
    }

    @Override
    public void remove(int toRemove) {
        int index = indexOf(toRemove);
        if(index == -1) {
            System.out.println("没有这个数字");
            return;
        }
        for (int i = index; i < usedSize; i++) {
            elem[i] = elem[i + 1];
        }
        usedSize--;
    }

    @Override
    public int size() {
        return usedSize;
    }

    @Override
    public void clear() {
        usedSize = 0;
    }

    @Override
    public void display() {
        for (int i = 0; i < usedSize; i++) {
            System.out.print(elem[i] + " ");
        }
        System.out.println();
    }
}
